package com.andkulikov.transitionseverywhere;

import org.android.animation.AnimationUtil;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.RadioGroup;

import com.linfaxin.transitionplayer.transitions.ChangeAlpha;
import com.linfaxin.transitionplayer.transitions.ChangeBackground;
import com.linfaxin.transitionplayer.transitions.ChangeRotate;
import com.linfaxin.transitionplayer.transitions.ChangeScale;
import com.linfaxin.transitionplayer.transitions.ChangeTextColor;
import com.linfaxin.transitionplayer.transitions.ChangeTransition;
import com.transitionseverywhere.ChangeBounds;
import com.transitionseverywhere.ChangeImageTransform;
import com.transitionseverywhere.Fade;
import com.transitionseverywhere.Scene;
import com.transitionseverywhere.Slide;
import com.transitionseverywhere.TransitionManager;
import com.transitionseverywhere.TransitionSet;

public class MainActivity extends Activity implements
		RadioGroup.OnCheckedChangeListener {

	private ViewGroup mSceneRoot;
	private Scene mCoinScene2;
	private Scene mCoinScene1;
	private Scene mCoinScene3;
	private Scene mCoinScene4;
	private ColorDrawable windowBG;
	private Scene mCoinScene3_2;
	private Scene mCoinScene3_1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_basic_transitions);

		RadioGroup radioGroup = (RadioGroup) findViewById(R.id.select_scene);
		radioGroup.setOnCheckedChangeListener(this);
		mSceneRoot = (ViewGroup) findViewById(R.id.scene_root);
		mCoinScene1 = Scene.getSceneForLayout(mSceneRoot, R.layout.coin_scene1, this);
		mCoinScene2= Scene.getSceneForLayout(mSceneRoot, R.layout.coin_scene2, this);
		mCoinScene3= Scene.getSceneForLayout(mSceneRoot, R.layout.coin_scene3, this);
		mCoinScene3_1= Scene.getSceneForLayout(mSceneRoot, R.layout.coin_scene3_1, this);
		mCoinScene3_2= Scene.getSceneForLayout(mSceneRoot, R.layout.coin_scene3_2, this);
		mCoinScene4= Scene.getSceneForLayout(mSceneRoot, R.layout.coin_scene4, this);
		windowBG = new ColorDrawable(0x00000000);
		getWindow().setBackgroundDrawable(windowBG);
		startCoinAnimation();
	}
	private void startCoinAnimation() {
		int time1 = 1000;//1秒后开始投币动画
		int time2 = time1+300;//场景1持续300秒
		int time3 = time2+300+800;//场景2持续300毫秒 1300毫秒为中间停留时间
		int time4 = time3+1000+800;//场景3持续300*3毫秒
		Handler animationTimer = new Handler();
		animationTimer.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				goScence1();
				
			}
		},time1);
		animationTimer.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				goScence2();
				
			}
		},time2);
		animationTimer.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				goScence3();
				
			}
		},time3);
		animationTimer.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				goScence4();
				
			}
		},time4);
		
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
	int mCurrentWindowAlpha = 0;
	final Handler handler = new Handler();
	public void alphaWindow(final int dstAlpha) {
		handler.removeCallbacksAndMessages(null);
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				if(dstAlpha>0){
					mCurrentWindowAlpha+=10;
					if (mCurrentWindowAlpha>dstAlpha) {
						return;
					}
				}else{
					mCurrentWindowAlpha-=10;
					if (mCurrentWindowAlpha<0) {
						return;
					}
				}
				windowBG.setColor(0x00000000|(mCurrentWindowAlpha<<24));
				getWindow().setBackgroundDrawable(windowBG);
				handler.postDelayed(this, 50);
				
			}
		});
	}


	@Override
	public void onCheckedChanged(final RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.select_scene_1: {
			goScence1();
			break;
		}
		case R.id.select_scene_2: {
			goScence2();
			break;
		}
		case R.id.select_scene_3: {
			goScence3();
			break;
		}
		case R.id.select_scene_4: {
			goScence4();
			break;
		}
		}
	}


	public void goScence4() {
		TransitionSet set = new TransitionSet();
		set.addTransition(new ChangeBounds());
		set.addTransition(new ChangeImageTransform());
		set.addTransition(new Fade(Fade.OUT));
		set.addTransition(new ChangeTransition());
		set.setOrdering(TransitionSet.ORDERING_TOGETHER);
		set.setInterpolator(new AccelerateDecelerateInterpolator());
		set.setDuration(300);

		TransitionManager.go(mCoinScene4,set);
		alphaWindow(0);
	}


	public void goScence3() {
		TransitionSet set = new TransitionSet();
		Slide slide = new Slide(Gravity.LEFT);
		slide.addTarget(R.id.transition_title);
		set.addTransition(slide);
		set.addTransition(new ChangeBounds());
		set.addTransition(new ChangeTransition());
		set.addTransition(new ChangeImageTransform());
		set.addTransition(new Fade(Fade.OUT));
		set.setOrdering(TransitionSet.ORDERING_TOGETHER);
		set.setDuration(300);
		TransitionManager.go(mCoinScene3,set);
		AnimationUtil.goneAfterAnimation(MainActivity.this, R.id.coin_circle, R.anim.coin_cricle_out);
		AnimationUtil.goneAfterAnimation(MainActivity.this, R.id.coin_coin, R.anim.coin_fadeout);
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				goScence3_1();
			}
		}, 300);
		
	
		
		
	}
	public void goScence3_1() {
		TransitionSet set = new TransitionSet();
		Slide slide = new Slide(Gravity.LEFT);
		slide.addTarget(R.id.transition_title);
		set.addTransition(slide);
		set.addTransition(new ChangeBounds());
		set.addTransition(new ChangeTransition());
		set.addTransition(new ChangeImageTransform());
//		set.addTransition(new Fade(Fade.OUT));
		set.setOrdering(TransitionSet.ORDERING_TOGETHER);
		set.setDuration(300);
		TransitionManager.go(mCoinScene3_1,set);
		AnimationUtil.goneAfterAnimation(MainActivity.this, R.id.coin_slot_light, R.anim.coin_blink);
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				goScence3_2();
			}
		}, 310);
	}
	public void goScence3_2() {
		TransitionSet set = new TransitionSet();
		Slide slide = new Slide(Gravity.LEFT);
		slide.addTarget(R.id.transition_title);
		set.addTransition(slide);
		set.addTransition(new ChangeBounds());
		set.addTransition(new ChangeTransition());
		set.addTransition(new ChangeImageTransform());
		set.addTransition(new Fade(Fade.OUT));
		set.setOrdering(TransitionSet.ORDERING_TOGETHER);
		set.setDuration(500);
		TransitionManager.go(mCoinScene3_2,set);
	}


	public void goScence2() {
		TransitionSet set = new TransitionSet();
		Slide slide = new Slide(Gravity.LEFT);
		slide.addTarget(R.id.transition_title);
		set.addTransition(slide);
		set.addTransition(new ChangeBounds());
		set.addTransition(new ChangeImageTransform());
		set.setOrdering(TransitionSet.ORDERING_TOGETHER);
		set.setDuration(300);
		set.setInterpolator(new OvershootInterpolator());
		TransitionManager.go(mCoinScene2,set);
		AnimationUtil.goneAfterAnimation(this, R.id.coin_circle, R.anim.coin_cricle);
		AnimationUtil.frameAnimation(this, R.id.coin_coin, R.anim.coin_rotate);
	}


	public void goScence1() {
		TransitionSet set = new TransitionSet();
		set.setOrdering(TransitionSet.ORDERING_TOGETHER);
		set. setDuration(300);
		ChangeBounds changeBounds = new ChangeBounds();
		ChangeAlpha changeAlpha = new ChangeAlpha();
		ChangeRotate changeRotate = new ChangeRotate();
		ChangeScale changeScale = new ChangeScale();
		ChangeTransition changeTransition = new ChangeTransition();
		ChangeBackground changeBackground = new ChangeBackground();
		ChangeTextColor changeTextColor = new ChangeTextColor();
		Fade fadeOut = new Fade(Fade.OUT);
		Fade fadeIn = new Fade(Fade.IN);
		set. addTransition(changeBounds);
		set. addTransition(changeAlpha);
		set. addTransition(changeRotate);
		set. addTransition(changeScale);
		set. addTransition(changeTransition);
		set. addTransition(changeBackground);
		set.addTransition(changeTextColor);

		set.addTransition(fadeOut);
		set. addTransition(fadeIn);

		set.setInterpolator(new LinearInterpolator());
		TransitionManager.go(mCoinScene1,set);
		alphaWindow(180);
	}

}
